﻿using System;
using System.Runtime.Caching;

namespace DoNet.Utils
{
    public class CacheUtil
    {
        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        public static object GetCache(string CacheKey)
        {
            MemoryCache cache = MemoryCache.Default;
            return cache.Get(CacheKey);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        public static void SetCache(string CacheKey, object obj)
        {
            MemoryCache cache = MemoryCache.Default;
            CacheItemPolicy policy = new CacheItemPolicy()
            {

            };
            cache.Set(CacheKey, obj, policy);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        /// <param name="Timeout"></param>
        public static void SetCache(string CacheKey, object obj, TimeSpan Timeout)
        {
            MemoryCache cache = MemoryCache.Default;
            DateTimeOffset referenceDateTime = DateTimeOffset.Now; // 参考时间点
            DateTimeOffset resultDateTime = referenceDateTime.Add(Timeout); // 将 TimeSpan 添加到参考时间点
            cache.Set(CacheKey, obj, resultDateTime);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        /// <param name="expire"></param>
        public static void SetCache(string CacheKey, object obj, int expire)
        {
            MemoryCache cache = MemoryCache.Default;
            //表示永不过期
            if (expire == 0)
            {
                CacheItemPolicy policy = new CacheItemPolicy()
                {

                };
                cache.Set(CacheKey, obj, policy);
            }
            else
            {
                DateTimeOffset resultDateTime = DateTimeOffset.Now.AddMilliseconds(expire);
                CacheItemPolicy policy = new CacheItemPolicy()
                {
                    AbsoluteExpiration = resultDateTime,
                    SlidingExpiration = MemoryCache.NoSlidingExpiration
                };
                cache.Set(CacheKey, obj, policy);
            }
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        /// <param name="obj">缓存的对象</param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        public static void SetCache(string CacheKey, object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            MemoryCache cache = MemoryCache.Default;
            CacheItemPolicy policy = new CacheItemPolicy()
            {
                AbsoluteExpiration = absoluteExpiration,
                SlidingExpiration = slidingExpiration
            };
            cache.Set(CacheKey, obj, policy);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        /// <param name="CacheKey">对象的键值</param>
        public static void RemoveCache(string CacheKey)
        {
            MemoryCache cache = MemoryCache.Default;
            cache.Remove(CacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            MemoryCache cache = MemoryCache.Default;
            foreach (var item in cache)
            {
                cache.Remove(item.Key);
            }
        }
    }

}